package cn.lifeaid.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.lifeaid.db.SysUser;
import cn.lifeaid.filter.CurrentUser;
import cn.lifeaid.service.SysResourceService;
import cn.lifeaid.service.SysUserService;

@Controller
public class LoginController {

	@Autowired
	private SysUserService userService;
	@Autowired
	private SysResourceService resourceService;

	/**
	 * 登录页面
	 * 
	 * @param request
	 * @param uiModel
	 * @return
	 */
	@RequestMapping("/login")
	public String showLoginForm(HttpServletRequest request, Model uiModel) {
		String exceptionClassName = (String) request
				.getAttribute("shiroLoginFailure");
		String error = null;
		if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
			error = "用户名/密码错误";
		} else if (IncorrectCredentialsException.class.getName().equals(
				exceptionClassName)) {
			error = "用户名/密码错误";
		} else if (exceptionClassName != null) {
			error = "其他错误：" + exceptionClassName;
		}
		uiModel.addAttribute("error", error);
		return "login";
	}
	@RequestMapping("/logout")
	public String logout(){
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect:/login";
	}

	/**
	 * index页面
	 * 
	 * @param loginUser
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	public String index(@CurrentUser SysUser loginUser, Model model) {

		return "index";
	}

	@RequestMapping("/welcome")
	public String welcome() {
		return "welcome";
	}

}
